package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.ResetPasswordBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.MD5;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ForgetSetNewPassActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    private Unbinder unbiner;
    private String ctoken;
    private String mobile;


    @OnTextChanged({R.id.edit_pass, R.id.edit_repass})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editRepass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_new_pass);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("重新设置密码");
        ctoken = getIntent().getStringExtra("ctoken");
        mobile = getIntent().getStringExtra("mobile");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private boolean regex() {
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        return UI.toString(editPass).matches(regex);
    }

    public void getResetPassword(String pass) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ResetPasswordBean bean = new ResetPasswordBean();
        bean.mobile = mobile;
        bean.token = ctoken;
        bean.password = MD5.getMD5str(pass);
        Api.api_service.getResetPassword(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    finish();

                });
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        if (!regex()) {
            ToastUtil.makeText(this, "至少6位数字加字母组合");
            return;
        }
        if (!UI.toString(editRepass).equals(UI.toString(editPass))) {
            ToastUtil.makeText(this, "两次输入密码不一致");
            return;
        }
        getResetPassword(UI.toString(editPass));

    }
}
